package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 分页查询
     * @param dto
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page page = setmealMapper.page(dto);

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        //数据拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //设置默认状态，停售
        setmeal.setStatus(0);
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        //得到套餐下的菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!setmealDishes.isEmpty()){
            setmealDishes.forEach(i -> {
                i.setSetmealId(id);
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //数据拷贝
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //删除套餐和菜品的关系
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //添加套餐和菜品的关系
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(!setmealDishes.isEmpty()){
            setmealDishes.forEach(i -> {
                i.setSetmealId(setmealDTO.getId());
            });
            setmealDishMapper.insertBatch(setmealDishes);
        }
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐
        Setmeal setmeal = setmealMapper.getById(id);
        //查询套餐下，所有的菜品
        List<SetmealDish> list = setmealDishMapper.getBySetmealId(id);
        //拷贝数据
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(list);
        return setmealVO;
    }

    /**
     * 更改套餐状态
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        if(Objects.equals(status, StatusConstant.DISABLE)){
            setmealMapper.update(setmeal);
        }else if(Objects.equals(status, StatusConstant.ENABLE)){
            List<SetmealDish> SetmealId = setmealDishMapper.getBySetmealId(id);
            List<Long> dishIds = SetmealId
                                    .stream()
                                    .map(SetmealDish::getDishId)
                                    .collect(Collectors.toList());
            if(!dishIds.isEmpty()){
                List<Dish> dishs = dishMapper.getByIds(dishIds);
                dishs.forEach(i -> {
                    if(Objects.equals(i.getStatus(), StatusConstant.DISABLE)){
                        throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }

            setmealMapper.update(setmeal);
        }

    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {

        //查看套餐的状态
        List<Setmeal> setmeals = setmealMapper.getByIds(ids);
        setmeals.forEach(i->{
            if(i.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        //删除套餐
        setmealMapper.deleteBatch(ids);
        //删除套餐和菜品的关系
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 查询套餐
     * @param categoryId
     * @return
     */
    @Override
    public List<Setmeal> getList(Long categoryId) {

        return setmealMapper.getByCateGoryAndStatus(categoryId);
    }

    /**
     * 根据套餐id查询菜品
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItems(Long id) {
        List<SetmealDish> bySetmealId = setmealDishMapper.getBySetmealId(id);
        //拿到所有dish id
        List<Long> dishIds = bySetmealId.stream().map(SetmealDish::getDishId).collect(Collectors.toList());
        List<Dish> byIds = dishMapper.getByIds(dishIds);
        //拿到所有起售的菜品
        List<Dish> collect = byIds.stream().filter(i -> i.getStatus() != 0).collect(Collectors.toList());
        int size = collect.size();
        List<DishItemVO> list = new ArrayList<>(size);
        collect.forEach(i->{
            DishItemVO build = DishItemVO.builder()
                    .description(i.getDescription())
                    .image(i.getImage())
                    .name(i.getName())
                    .copies(size)
                    .build();
            list.add(build);

        });

        return list;
    }
}
